﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OTDR_OPT
{
    /// <summary>
    /// FiberCorrectionEdit.xaml 的交互逻辑
    /// </summary>
    public partial class FiberCorrectionEdit : Window
    {
        public FiberCorrectionEdit()
        {
            InitializeComponent();
            InitWindow();
        }


        //读取按钮点击时，读取功率值，显示在界面上
        private void ReadButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            string buttonName = button.Name;
            List<CorrectionControl> correctionControlsList = null;
            switch (buttonName)
            {
                case "ReadButton1":
                    correctionControlsList = GetCorrectionList(FiberType.Single);
                    foreach (CorrectionControl correctionControl in correctionControlsList) {
                        string wavelength = correctionControl.Wavelength;
                        if (wavelength == null)
                        {
                            continue;
                        }
                        double power = RunTest.GetInstance().ReadFiber1Power(wavelength);
                        correctionControl.FirstPower = power.ToString();
                    }
                    ReadButton2.IsEnabled = true; //先读取通道1，后读取通道2
                    break;
                case "ReadButton2":
                    correctionControlsList = GetCorrectionList(FiberType.Single);
                    foreach (CorrectionControl correctionControl in correctionControlsList)
                    {
                        string wavelength = correctionControl.Wavelength;
                        if (wavelength == null)
                        {
                            continue;
                        }
                        double power = RunTest.GetInstance().ReadFiber2Power(wavelength);
                        correctionControl.SecondPower = power.ToString();
                    }
                    break;
                case "ReadButton3":
                    correctionControlsList = GetCorrectionList(FiberType.Multi);
                    foreach (CorrectionControl correctionControl in correctionControlsList)
                    {
                        string wavelength = correctionControl.Wavelength;
                        if (wavelength == null)
                        {
                            continue;
                        }
                        double power = RunTest.GetInstance().ReadFiber3Power(wavelength);
                        correctionControl.FirstPower = power.ToString();
                    }
                    ReadButton4.IsEnabled = true; //先读取通道1，后读取通道2
                    break;
                case "ReadButton4":
                    correctionControlsList = GetCorrectionList(FiberType.Multi);
                    foreach (CorrectionControl correctionControl in correctionControlsList)
                    {
                        string wavelength = correctionControl.Wavelength;
                        if (wavelength == null)
                        {
                            continue;
                        }
                        double power = RunTest.GetInstance().ReadFiber4Power(wavelength);
                        correctionControl.SecondPower = power.ToString();
                    }
                    break;

            }
        }


        //获取光纤修正值控件清单
        private List<CorrectionControl> GetCorrectionList(FiberType fiberType)
        {
            List<CorrectionControl> correctionControlsList = new List<CorrectionControl>();
            switch (fiberType)
            {
                case FiberType.Single:
                    foreach(object obj in SingleFiberGrid.Children)
                    {
                        correctionControlsList.Add((CorrectionControl)obj);
                    }
                    break;
                case FiberType.Multi:
                    foreach (object obj in MultiFiberGrid.Children)
                    {
                        correctionControlsList.Add((CorrectionControl)obj);
                    }
                    break;
            }
            return correctionControlsList;
        }


        //保存配置内容
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            List<FiberPower> fiberPowerList = new List<FiberPower>();
            #region 保存单模数据
            List<CorrectionControl> singleCorrectionControlList = GetCorrectionList(FiberType.Single);
            foreach(CorrectionControl control in singleCorrectionControlList)
            {
                FiberPower fiberPower = new FiberPower();
                fiberPower.TypeOfFiber = FiberType.Single;
                string wavelength = control.Wavelength;
                double power1 = Convert.ToDouble(control.FirstPower);
                double power2 = Convert.ToDouble(control.SecondPower);
                if (wavelength == null || control.FirstPower == null || control.SecondPower==null) //波长、纤1的功率、纤2的功率有任何一个是空的，不保存数据
                {
                    continue;
                }
                fiberPower.Wavelength = wavelength;
                fiberPower.Fiber1Power = power1;
                fiberPower.Fiber2Power = power2;
                fiberPowerList.Add(fiberPower);
            }
            #endregion

            #region 保存多模数据
            List<CorrectionControl> mutliCorrectionControlList = GetCorrectionList(FiberType.Multi);
            foreach (CorrectionControl control in mutliCorrectionControlList)
            {
                FiberPower fiberPower = new FiberPower();
                fiberPower.TypeOfFiber = FiberType.Multi;
                string wavelength = control.Wavelength;
                double power1 = Convert.ToDouble(control.FirstPower);
                double power2 = Convert.ToDouble(control.SecondPower);
                if (wavelength == null || control.FirstPower == null || control.SecondPower == null) //波长、纤1的功率、纤2的功率有任何一个是空的，不保存数据
                {
                    continue;
                }
                fiberPower.Wavelength = wavelength;
                fiberPower.Fiber1Power = power1;
                fiberPower.Fiber2Power = power2;
                fiberPowerList.Add(fiberPower);
            }
            #endregion
            FiberCorrectionManagement.GetInstance().SaveFiberPowers(fiberPowerList);
            MessageBox.Show("保存光纤修正值完成","保存成功",MessageBoxButton.OK,MessageBoxImage.Information);
        }


        //读取配置文件内容，初始化窗口
        private void InitWindow()
        {
            List<FiberPower> fiberPowerList = FiberCorrectionManagement.GetInstance().GetFiberPowerList();
            List<FiberPower> singleFiberPowerList = new List<FiberPower>(); //单模数据
            List<FiberPower> multiFiberPowerList = new List<FiberPower>(); //多模数据
            foreach (FiberPower fiberPower in fiberPowerList)
            {
                FiberType fiberType = fiberPower.TypeOfFiber;
                #region 如果是单模，添加数据到单模清单中，否则添加到多模清单中
                if (fiberType == FiberType.Single)
                {
                    singleFiberPowerList.Add(fiberPower);
                }
                else
                {
                    multiFiberPowerList.Add(fiberPower);
                }
                #endregion
            }
            #region 添加单模控件
            int singleCount = singleFiberPowerList.Count();
            for(int i = 0; i < singleCount; i++)
            {
                FiberPower fiberPower = singleFiberPowerList[i];
                string wavelength = fiberPower.Wavelength;
                double power1 = fiberPower.Fiber1Power;
                double power2 = fiberPower.Fiber2Power;
                UIElementCollection correctionControls = SingleFiberGrid.Children; //获取控件清单
                CorrectionControl control = (CorrectionControl)correctionControls[i];
                control.Wavelength = wavelength;
                control.FirstPower = power1.ToString();
                control.SecondPower = power2.ToString();
                if (i < singleCount-1)
                {
                    control.AddControl();
                }
            }
            #endregion
            #region 添加多模控件
            int multiCount = multiFiberPowerList.Count();
            for(int i=0;i< multiCount; i++)
            {
                FiberPower fiberPower = multiFiberPowerList[i];
                string wavelength = fiberPower.Wavelength;
                double power1 = fiberPower.Fiber1Power;
                double power2 = fiberPower.Fiber2Power;
                UIElementCollection correctionControls = MultiFiberGrid.Children; //获取控件清单
                CorrectionControl control = (CorrectionControl)correctionControls[i];
                control.Wavelength = wavelength;
                control.FirstPower = power1.ToString();
                control.SecondPower = power2.ToString();
                if (i < multiCount - 1)
                {
                    control.AddControl();
                }
            }
            #endregion
        }
    }
}
